﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NP.BPMReportPlatform.BPMPlatform;
using NP.BPMReportPlatform.CustomerProfileConfig.Dto;
using NP.BPMReportPlatform.DBHelper;
using NP.BPMReportPlatform.Entities.BPM_Business;
using NP.BPMReportPlatform.Entities.BPM_Platform;
using NP.BPMReportPlatform.Hana;
using NP.BPMReportPlatform.Hana.Dto;
using NP.BPMReportPlatform.Utils;
using NP.BPMReportPlatform.ZHLC.Dto;
using NP.Enterprise.Utils.HanaOdbc;
using SqlSugar;
using Volo.Abp.Application.Dtos;

namespace NP.BPMReportPlatform.Hana
{
    public class HanaService : BPMReportPlatformAppService, IHanaService
    {
        private readonly IHanaOdbcAppService _hanaAppService;

        public HanaService(IHanaOdbcAppService hanaAppService)
        {
            _hanaAppService = hanaAppService;
        }

        /// <summary>
        /// 根据客户编码或名称查询客户信息（默认展示10条匹配的数据） 去掉冻结客户
        /// KNVV-LOEVM 删除标记
        /// KNVV-AUFSD 订单冻结
        /// KNVV-LIFSD 交货冻结
        /// KNVV-FAKSD 发票冻结
        /// </summary>
        /// <param name="customerCode">客户编码</param>
        /// <returns></returns>
        public async Task<IList<GetKNA1CustomerOutput>> GetKNA1CustomerAsync(string customerCode)
        {
            string sql = $@" SELECT DISTINCT K.KUNNR as Code ,K.NAME1 as Name 
            FROM ERP.KNA1 K
            LEFT JOIN ERP.KNVV V ON K.KUNNR = V.KUNNR
            WHERE(K.KUNNR LIKE '%{customerCode}%' or K.NAME1 LIKE '%{customerCode}%') AND V.LOEVM = '' AND V.AUFSD = '' AND V.LIFSD = '' AND V.FAKSD = ''";
            return await _hanaAppService.GetListAsync<GetKNA1CustomerOutput>(sql);
        }

        /// <summary>
        /// 根据送达方编码找售达方编码
        /// </summary>
        /// <param name="business">事业部</param>
        /// <param name="code">送达方编码</param>
        /// <returns></returns>
        public async Task<IList<SDFInfoOutput>> GetSDFInfo(string business,string code)
        {
            string sql = $@"SELECT DISTINCT KNVP.KUNN2 SDFCODE1,  KNA1.NAME1 SDFNAME1, KNVP.KUNNR AS SDFCODE2,   k2.NAME1 SDFNAME2      
                        FROM ERP.KNA1 KNA1                
                        LEFT JOIN ERP.KNVP KNVP ON KNA1.KUNNR = KNVP.KUNN2                
                        LEFT JOIN ERP.KNA1 k2 ON k2.KUNNR = KNVP.KUNNR            
                        LEFT JOIN ERP.TZONT TZONT ON TZONT.ZONE1 = KNA1.LZONE                
                        LEFT JOIN ERP.T005U T005U ON T005U.BLAND = KNA1.REGIO                
                        WHERE KNVP.PARVW = 'WE' AND TZONT.SPRAS = '1' AND T005U.MANDT = '500' AND T005U.SPRAS = '1'                 
                        AND KNVP.VTWEG = '{business}' 
                        AND KNVP.KUNN2 = '{code}'";
            return await _hanaAppService.GetListAsync<SDFInfoOutput>(sql);
        }
    }
}